import { _decorator, Component, instantiate, Label, Node, Prefab } from 'cc';
import { Report, SideType } from '../entity/Report';

import { reportInfo_Attack } from '../entity/reportInfo_Attack';
import { ArmyLoseItem } from './ArmyLoseItem';
import { Soldier } from '../entity/Soldier';
import { Army } from '../entity/Army';
const { ccclass, property } = _decorator;

@ccclass('ArmyLose')
export class ArmyLose extends Component {
    @property(Label)
    tittle:Label
    @property(Node)
    layout:Node
    @property(Prefab)
    armyLoseItem:Prefab

    start() {

    }

    setData(report: Report, side:SideType, info: reportInfo_Attack) {
     
        if(side == SideType.Attack){
            this.render(info.attackArmy, info.lostAttackArmy)
        } else {
            this.render(info.defenseArmy, info.lostDefenseArmy)
        }
    }

    render(list:Army, lost:Army){
        for(let i = 0; i< list.soldierList.length; i++){
            let node = instantiate(this.armyLoseItem)
            

            let so = list.soldierList[i]
            let count = so.count
            let loseCount = lost.soldierList[i].count
            if(count == 0){
                continue
            }
            this.layout.addChild(node)
            node.getComponent(ArmyLoseItem).setData(so, count, loseCount)
        }
    }

  

    setHelpArmy(list:Soldier[], lost: Soldier[]){
        let count = 0
        for(let item of list){
            count += item.count
        }
        if(count == 0){
            this.node.destroy()
            return
        }


        this.tittle.string = '其他村庄的支援'


        for(let i = 0; i<list.length;i++){
            let node = instantiate(this.armyLoseItem)
            this.layout.addChild(node)

            node.getComponent(ArmyLoseItem).setData(list[i], list[i].count, lost[i].count)
        }
    }
}


